<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use App\Models\Bus\Bus;
use App\Models\Insure\Insure;
use App\Models\Bus\BusQualificationFile;
use App\Models\Insure\InsureFile;

class updateBusFile extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update:busFile';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新车辆保险行驶证';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 7月检车行驶证
        $adminPath = public_path('storage/bus/行驶证扫描件');
        $adminFiles = File::allFiles($adminPath);
        $adminFiles = $this->getFilesNamePath($adminFiles, '行驶证');
        $this->updateBusFile($adminFiles);
        // 保险扫描件
        $adminPath = public_path('storage/bus/保险扫描件');
        $adminFiles = File::allFiles($adminPath);
        $adminFiles = $this->getFilesNamePathAppendixs($adminFiles, '保险');
        $this->updateInsureFile($adminFiles);
    }

    public function getFilesNamePath($files, $type)
    {
        $data = [];
        $basePath = config('app.url').'/storage/bus/行驶证扫描件';
        foreach ($files as $k => $file) {
            $baseName = $file->getBasename();
            $data[] = [
                'name' => explode('.', $baseName)[0],
                'path' => $basePath.$type.'/'.$file->getFileName(),
            ];
        }

        return collect($data);
    }

    public function getFilesNamePathAppendixs($files)
    {
        $data = [];
        $basePath = config('app.url').'/storage/bus/';
        foreach ($files as $k => $file) {
            $name = $file->getRelativePath();
            $baseName = $file->getBasename();
            $extension = $file->getExtension();
            $pathname = explode('/', $file->getRealPath());
            $size = $file->getSize();
            $storage = '保险扫描件/';

            $data[] = [
                'type'      => 'insure',
                'file_type' => $extension,
                'file_size' => $size,
                'file_name' => explode('.', $baseName)[0],
                'file_path' => $basePath.$storage.$name.'/'.$file->getFileName(),
                'user_id'   => 1,
                'name'      => $pathname[count($pathname) - 2],
            ];
        }

        return collect($data);
    }

    public function updateBusFile($files)
    {
        foreach ($files as $kf => $vf) {
            $photo=Bus::where('license_plate_num', $vf['name'])->update(['vehicle_travel_license_url'=>$vf['path']]);
            if ($photo) {
                $this->info("- Yes: {$vf['name']}");
            } else {
                $this->info("- No: {$vf['name']}");
            }
        }
    }

    public function updateInsureFile($files)
    {
        foreach ($files as $kf => $vf) {
            $bus_id = Bus::query()->where('license_plate_num', $vf['name'])->value('id');
            $photo = Insure::query()->where('bus_id', $bus_id)->first();
            if ($photo) {
                unset($vf['name']);
                $personnelFile = new InsureFile($vf);
                $personnelFile->insure_id = $photo['id'];
                $personnelFile->save();
                $this->info("- Yes: {$photo['bus_id']}");
            } else {
                $this->info("- No: {$vf['name']}");
            }
        }
    }
}
